package shayu.seckill.controller;

import io.renren.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import shayu.seckill.service.SeckillService;
import shayu.seckill.to.SeckillSkuRedisTo;

import java.awt.desktop.UserSessionListener;
import java.util.List;

@RequestMapping("seckill")
@Controller
public class SeckillController {

    @Autowired
    private SeckillService seckillService;

    /**
     * 返回当前商品可以参与秒杀商品的信息
     *
     * @return
     */
    @GetMapping(value = "/getCurrentSeckillSkus")
    @ResponseBody
    public R getCurrentAeckillSkus() {
        List<SeckillSkuRedisTo> vos = seckillService.getCurrentAeckillSkus();
        return R.ok().setData(vos);
    }

    @GetMapping("/sku/seckill/{skuId}")
    @ResponseBody
    public R getSkuSeckillInfo(@PathVariable("skuId") Long skuId) {
        SeckillSkuRedisTo vos = seckillService.getSkuSeckillInfo(skuId);
        return R.ok().setData(vos);
    }

    @GetMapping("/kill")
    public String seckill(@RequestParam("killId") String killId,
                          @RequestParam("key") String key,
                          @RequestParam("num") Integer num,
                          Model model) {
        String orderSn = null;
        try {
            // 判断是否登录
            orderSn = seckillService.kill(killId, key, num);
            model.addAttribute("orderSn", orderSn);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return "success";
    }
}
